#pragma once

#include <string>

#include <cstring>
#include <cstdlib>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "log.hpp"

extern Log log;

static constexpr int backlog = 10;

class Socket
{
public:
    enum Socket_Error
    {
        SOCKET_CREATE_ERROR = 1,
        SOCKET_BIND_ERROR,
        SOCKET_LISTEN_ERROR,
        SOCKET_ACCEPT_ERROR
    };

    Socket(int domain = AF_INET)
        : _socket(-1)
        , _domain(domain)
    {}

    void Create(int type)
    {
        _socket = socket(_domain, type, 0);
        if (_socket < 0)
        {
            log(Fatal, "socket create error, listen_sockfd: %d, errno: %d, errstring: %s", _socket, errno, strerror(errno));
            exit(SOCKET_CREATE_ERROR);
        }
        log(Info, "socket create success, listen_sockfd = %d", _socket);
        int opt = 1;
        setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)); //配置套接字,防止偶发性服务器无法立即重启
    }
    void Bind(uint16_t port, const std::string& ip = "0.0.0.0")
    {
        struct sockaddr_in local;
        local.sin_family = _domain;
        local.sin_port = htons(port);
        // inet_pton(_domain, ip.c_str(), &(local.sin_addr));
        local.sin_addr.s_addr = INADDR_ANY;
        if (bind(_socket, (struct sockaddr*)&local, sizeof(local)) < 0)
        {
            log(Fatal, "socket bind error, errno: %d, errstring: %s", errno, strerror(errno));
            exit(SOCKET_BIND_ERROR);
        }
        log(Info, "socket bind success");
    }
    int Listen()
    {
        if (listen(_socket, backlog) < 0)
        {
            log(Fatal, "socket listen error, errno: %d, errstring: %s", errno, strerror(errno));
            exit(SOCKET_LISTEN_ERROR);
        }
        log(Info, "socket listen success");
    }
    int Accept(std::string& clientip, uint16_t& clientport)
    {
        struct sockaddr_in client;
        socklen_t clientlen = sizeof(client);
        int sockfd = accept(_socket, (struct sockaddr*)&client, &clientlen);
        if (sockfd < 0)
        {
            log(Warning, "socket accept error, errno: %d, errstring: %s", errno, strerror(errno));
            return -1;
        }
        char ipstr[INET6_ADDRSTRLEN];
        inet_ntop(_domain, &(client.sin_addr), ipstr, INET6_ADDRSTRLEN);
        clientip = ipstr;
        clientport = ntohs(client.sin_port);
        log(Info, "get a new link..., sockfd: %d, clientip: %s, clientport: %d", sockfd, clientip.c_str(), clientport);
        return sockfd;
    }
    int Connect(const struct sockaddr* server, socklen_t len)
    {
        if (connect(_socket, server, len) < 0)
        {
            log(Warning, "socket connect error, errno: %d, errstring: %s", errno, strerror(errno));
            return -1;
        }
        return _socket;
    }
    void Close()
    {
        if (_socket > 0)
            close(_socket);
        _socket = -1;
    }

    ~Socket()
    {
        if (_socket > 0)
            close(_socket);
        _socket = -1;
    }
private:
    int _socket;
    int _domain;
};